﻿using DataGridViewCellExtLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class MainForm : Form
    {
        private const int _cellIndex = 3;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitUsers();
            SetOperation();
        }

        private void InitUsers()
        {
            int index = 1;
            var list = new List<UserInfo>
            {
                new UserInfo
                {
                    Id = index++,
                    UserName = "张三",
                    OperateType = index % 2
                },
                new UserInfo
                {
                    Id = index++,
                    UserName = "李四",
                    OperateType = index % 2
                },
                new UserInfo
                {
                    Id = index++,
                    UserName = "王五",
                    OperateType = index % 2
                },
                new UserInfo
                {
                    Id = index++,
                    UserName = "赵六",
                    OperateType = index % 2
                },
                new UserInfo
                {
                    Id = index++,
                    UserName = "韩七",
                    OperateType = index % 2
                },
                new UserInfo
                {
                    Id = index++,
                    UserName = "孙八",
                    OperateType = index % 2
                },
            };

            foreach (var item in list)
            {
                userInfoBindingSource.Add(item);
            }
        }

        private void SetOperation()
        {
            var list = (BindingList<UserInfo>)userInfoBindingSource.List;
            int index = 0;
            foreach (var item in list)
            {
                var cell = dataGridView.Rows[index].Cells[_cellIndex];
                var panel = cell.AddPanelControl(dataGridView);
                panel.BackColor = Color.Transparent;
                Control[] controls = null;
                if (item.OperateType == 0)
                {
                    int diffY = 3;
                    int height = panel.Height - diffY * 2;
                    int width = 100;
                    int x1 = 20;
                    int x2 = panel.Width - x1 - width;
                    
                    controls = new Control[]
                        { 
                            new RadioButton
                            { 
                                Text = "选择1",
                                Width = width,
                                Height = height,
                                Location = new Point(x1, diffY)
                            },
                            new RadioButton
                            {
                                Text = "选择2",
                                Width = width,
                                Height = height,
                                Location = new Point(x2, diffY)
                            }
                        };
                }
                else
                {
                    int diffX = 10;
                    var textBox = new TextBox
                    {
                        Width = panel.Width - diffX * 2,
                        Text = "请输入"
                    };
                    textBox.Location = new Point(diffX, (panel.Height - textBox.Height) / 2);
                    controls = new Control[]
                    {
                        textBox
                    };
                }
                panel.Controls.AddRange(controls);
                index++;
            }
        }
    }
}
